let tools = {};
// 数组按照数组对象列的某个键排序
tools.compare = (obj1, obj2) => {
  var val1 = obj1.k_id;
  var val2 = obj2.k_id;
  if (val1 < val2) {
    return -1;
  } else if (val1 > val2) {
    return 1;
  } else {
    return 0;
  }
}
// 对象按键排序
tools.objSort = (obj) => {
  const news_obj = {};
  Object.keys(obj).sort().forEach((key) => {
    news_obj[key] = obj[key];
  });
  return news_obj;
}
// 数组排序
tools.arrSort = (arr) =>{
  return arr.sort(tools.compare).map(item => {
    return tools.objSort(item)
  })
}
// 判断两个sku数组是否相同
tools.equal = (prevArr,nextArr) => {
  return JSON.stringify(tools.arrSort(prevArr)) === JSON.stringify(tools.arrSort(nextArr))
}
// 全局跳转函数
tools.header = function(url,type,login) {
	// login 等于true表示必须登录次才能进入的页面没登陆跳到登录页
	if (login) {
		if (uni.getStorageSync('token')) {
			uni.navigateTo({
				url: url
			});
		} else {
			uni.navigateTo({
				url: '/pages/user/wxlogin'
			});
		}
	} else {
		if (type && type == 2) {
			uni.switchTab({
				 url: url
			})
		} else {
			uni.navigateTo({
			    url: url
			});
		}
	}
}
tools.show = function(error) {
	uni.showToast({
	    title:error,
	    duration: 2000,
		icon:'none'
	});
}

tools.showModal = function(content,callback,...arg) {
	uni.showModal({
	    content:content,
		success:res => {
			if (res.confirm) {
				callback(...arg)
			}
		}
	});
}

export  {tools};
